-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inputs_for for liveview #52
Conversation
I put this back to draft because I'd like to change one thing. When rendering forms for polymorphic data types, especially in LiveView, a common use case would be a form in which you can dynamically add more entries and select the concrete type for each of those entries. This is not possible with the current For the new |
I made the described updates.
Please have a look. |
Great work @woylie ! Sorry but I've been away from Elixir for some time, it's been a looong looong time 😰 You confused me with something now, you're not passing the Do we even need the type at all even for the existing function? Does it have any use for any case? (again, been a long time, might miss something obvious 😂). |
Hi @mathieuprog! Before this PR, the library didn't have a function for determining the type automatically, and that's why it has to be passed to the old |
Could you add that change? I will just release a major version as there is a breaking change. |
Sure! |
When I just remove the For the new We can also keep the |
aaf415e
to
561ad3b
Compare
Which way? |
Okay got it. The /4 returns empty inputs. The /3 doesn't render anything, right? That's quite big change. Let's keep the /4 for now, but add a /3 version. |
I realize my own app depends on these empty fields lol. So keep the /4, add the /3. And I'll see with my own app how I should refactor it to only use /3, or we just keep /4 if the code is easier for getting these empty fields. |
You can do something like changeset = reminder_changeset(%Reminder{}, %{"channel" => %{"__type__" => "sms"}}) to initialize the form with empty fields.
It's a breaking change, but it's easy to work around, and it makes more sense to me. The static type isn't very useful in a polymorphic situation, unless you want to render a certain type by default if no value is given, but I find this behaviour a bit quirky and would rather initialize the changeset as described to do that. |
I see. We're initializing the changeset as described in our application to get those empty fields (did I mention you can do that? 😅 ). I'll update the PR to include both variants, and you can decide whether you want to deprecate |
Yep just keep the /4 for now and I'll try your suggestion in my app! I'll merge your PR in master as soon as it is ready. Can you comment here when it's ready for merge? Thanks! |
18afb3d
to
16569b5
Compare
Ok, I updated the PR. I replaced the |
You loop through generators: @generators [:not_polymorphic, :polymorphic, :polymorphic_with_type]
defp get_module(name, generator) when generator in [:polymorphic, :polymorphic_with_type],
do: Module.concat([PolymorphicEmbed, name])
defp get_module(name, :not_polymorphic),
do: Module.concat([PolymorphicEmbed.Regular, name])
for generator <- @generators do
# ...
end Now most of the tests will be run 3 times, including 2 times for the exact same code? |
It runs the same tests on both of the |
adds empty fields in case embed is nil
1.10.0 has been released but major update 2.0.0 coming tomorrow :D |
Thanks! |
Btw you change
Do you have any rule you made for yourself? |
Those are two rules that Credo has checks for:
I find both reasonable and usually have them enabled in my projects. |
Hello @woylie |
@mathieuprog I'll check tomorrow. |
Ty! The error I'm having when upgrading to 0.18 is the following:
This is really a mystery for me. |
Ah, that's easy, a bunch of functions were moved from |
In the docs the example says to use: use Phoenix.Component https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#sigil_H/2 However an |
|
@woylie I only noticed there were some inconsistencies in that PR following this open issue: I had to remove these clauses: %{"__type__" => type} ->
maybe_to_existing_atom(type)
%{__type__: type} ->
maybe_to_existing_atom(type) The issue is still open and waiting for feedback to add the right test. All the tests are running without the clauses above, so the code seemed useless, until we find the right test case. Just FYI |
I'll have to have a look at the application where we first added that, I don't remember why this was added. |
Ok note that there is a custom type field for |
After upgrading to 3.0.5, |
I'm sorry, on the other hand, nice that we have a failing case. I suspected it hence I reach out to you immediately so that we can fix it. I would say, just add the failing test, and I'll code the fix. I'm not an active user of LiveView, I don't grasp or fully remember the case we're talking about, so it would be nice to have an example that fails in the test. |
Most likely we should use the already existing function |
This adds
polymorphic_embed_inputs_for/3
for LiveView integration. The difference is that it doesn't use an anonymous function, and it doesn't automatically render hidden inputs. This follows the example ofPhoenix.HTML.Form.inputs_for/3
.I also handled some warnings and updated mix.lock.